package com.xiaoman.read.config;

import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.nodes.Node;
import org.jsoup.nodes.TextNode;

import java.util.List;

public class Chapter {
    public String title = "";
    public String content = "";


    public Chapter(String title, String content) {
        this.title = title;
        this.content = content;
    }

    public Chapter(String xml) {
        Document document = Jsoup.parse(xml);
        Element body = document.body();
        List<Node> nodes = body.childNodes();
        for (Node node : nodes) {
            if (node instanceof Element element) {
                //处理标题
                if (element.tagName().matches("h[1-6]")) {
                    title = element.text();
                } else {
                        content += element.text() + "\n"; // 使用 element.text() 提取纯文本
                }
            } else if (node instanceof TextNode textNode) {
                content += textNode.text() + "\n";
            }
        }
    }
}
